import 'package:flutter/cupertino.dart';
import 'package:multi_shop/models/cart_item.dart';
import 'package:multi_shop/network/api_client.dart';

import '../network/api_routes.dart';

class CartApi {
  final _http = HttpClient();
  // 获取列表
  Future<List<CartItem>> getList() async {
    final res = await _http.getJson(ApiRoutes.cartList);
    return (res["list"] as List).map((e) => CartItem.fromJson(e)).toList();
  }

  // 新增
  Future<void> create(int goodsId, int skuId, int count) async {
    return await _http.postJson(ApiRoutes.cartAdd, {
      'goodsId': goodsId,
      'skuId': skuId,
      'count': count,
    });
  }

  // 更新
  Future<void> update(int id, count) async {
    return await _http.putJson(ApiRoutes.cartUpdate, {
      "id": id,
      "count": count,
    });
  }

  // 删除
  Future<void> delete(List<int?> ids) async {
    await _http.deleteJson(ApiRoutes.cartDelete, query: {'ids': ids});
  }

  // 清空
  Future<void> clear() async {
    await _http.deleteJson(ApiRoutes.cartClear);
  }
}
